/**
 * @file bsp_log.c
 * @brief 日志API
 * @author wangh (wanghuan3037@fiberhome.com)
 * @version 1.0
 * @date 2020-05-12
 * @copyright Copyright (c) 2020  烽火通信
 ***************************************************************
 * @par 修改日志:
 * <table>
 * <tr><th>Date       <th>Version  <th>Author  <th>Description
 * <tr><td>2020-05-12 <td>1.0      <td>wangh   <td>内容
 * </table>
 */
#include "bsp_log.h"
#include <stdio.h>
#include "FreeRTOS.h"
#include "semphr.h"

#if (LOG_USES_UART)
#include "project_board.h"
#endif

#if EASYLOG_EN
/**
 * @brief EasyLogger 部件初始化
 */
void EasyLogger_init(void)
{
    /* close printf buffer */
	// setbuf(stdout, NULL);
    /* initialize EasyLogger */
    elog_init();
    /* 设置日志格式 */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL & ~ELOG_FMT_TIME & ~ELOG_FMT_P_INFO & ~ELOG_FMT_T_INFO & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_ALL & ~ELOG_FMT_TIME & ~ELOG_FMT_P_INFO & ~ELOG_FMT_T_INFO & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_ALL & ~ELOG_FMT_TIME & ~ELOG_FMT_P_INFO & ~ELOG_FMT_T_INFO & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_FUNC|ELOG_FMT_LINE);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_TIME & ~ELOG_FMT_P_INFO & ~ELOG_FMT_T_INFO & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_TIME & ~ELOG_FMT_P_INFO & ~ELOG_FMT_T_INFO & ~ELOG_FMT_FUNC);
    /* 使能日志颜色 */
	// elog_set_text_color_enabled(true);
    elog_start();	/* start EasyLogger */
}

#else

#ifndef LOG_USE_LOCK
#define LOG_USE_LOCK    0
#endif

#if LOG_USE_LOCK
static SemaphoreHandle_t 	logSemaphore;
#endif

/**
 * @brief 日志输出上锁
 */
void log_lock(void)
{
#if LOG_USE_LOCK
    register uint32_t __regIPSR     __ASM("ipsr");
    if (__regIPSR != 0)
    {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
        xSemaphoreTakeFromISR( logSemaphore, &xHigherPriorityTaskWoken );
    }
    else
        xSemaphoreTake(logSemaphore, portMAX_DELAY);
#endif
}

/**
 * @brief 日志输出解锁
 */
void log_unlock(void) 
{
#if LOG_USE_LOCK
    register uint32_t __regIPSR     __ASM("ipsr");
    if (__regIPSR != 0)
    {
        BaseType_t xHigherPriorityTaskWoken = pdFALSE;
        xSemaphoreGiveFromISR( logSemaphore, &xHigherPriorityTaskWoken );
    }
    else
        xSemaphoreGive( logSemaphore );
#endif
}

/**
 * @brief 日志输出16进制格式
 * @param  buf      	输出数据
 * @param  size       	输出数据长度
 */
void log_hexdump(uint8_t *buf, uint16_t size)
{
    uint16_t i;
    
    log_lock();
    for(i=0; i<size; i++)
    {
        log_printf("%02X ", buf[i]);
    }
    log_printf("\r\n");
    log_unlock();
}
#endif


/**
 * @brief 日志模块初始化
 */
void log_init(void)
{
#if (LOG_USES_RTT)
    SEGGER_RTT_Init();
#elif (LOG_USES_UART)
    #if (LOG_UART == 1)
    UartInit( DEBUG_UART, UART1_TX_PIN, UART1_RX_PIN, SHELL_BUF_SIZE );
    #elif (LOG_UART == 2)
    UartInit( DEBUG_UART, UART2_TX_PIN, UART2_RX_PIN, SHELL_BUF_SIZE );
    #elif (LOG_UART == 3)
    UartInit( DEBUG_UART, UART3_TX_PIN, UART3_RX_PIN, SHELL_BUF_SIZE );
    #elif (LOG_UART == 4)
    UartInit( DEBUG_UART, UART4_TX_PIN, UART4_RX_PIN, SHELL_BUF_SIZE );
    #elif (LOG_UART == 5)
    UartInit( DEBUG_UART, UART5_TX_PIN, UART5_RX_PIN, SHELL_BUF_SIZE );
    #endif
    UartConfig( DEBUG_UART, RX_TX, LOG_UART_BAUD, UART_8_BIT, UART_1_STOP_BIT, NO_PARITY, NO_FLOW_CTRL );
#endif
    
#if EASYLOG_EN
    EasyLogger_init();
#else
    #if LOG_USE_LOCK
    logSemaphore = xSemaphoreCreateBinary();	/* 创建日志输出锁 */
    xSemaphoreGive( logSemaphore );
    #endif
#endif
}



/*********** (C) COPYRIGHT 2020 FiberHome *****END OF FILE****/
